﻿using System;
using System.IO;
using System.Text;
using HslCommunication.Core;
using HslCommunication.LogNet;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 文件存储功能的基类，包含了文件存储路径，存储方法等
	/// </summary>
	/// <remarks>
	/// 需要继承才能实现你想存储的数据，比较经典的例子就是存储你的应用程序的配置信息，通常的格式就是xml文件或是json文件。具体请看例子：
	/// </remarks>
	/// <example>
	/// 下面举例实现两个字段的普通数据存储
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftFileSaveBaseExample.cs" region="SoftFileSaveBase1" title="简单示例" />
	/// 然后怎么调用呢？
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftFileSaveBaseExample.cs" region="Example" title="调用示例" />
	/// 如果你想实现加密存储，这样就不用关心被用户看到了。
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftFileSaveBaseExample.cs" region="SoftFileSaveBase2" title="加密示例" />
	/// 如果还是担心被反编译获取数据，那么这个密钥就要来自服务器的数据，本地不做存储。
	/// </example>
	// Token: 0x0200018C RID: 396
	public class SoftFileSaveBase : ISoftFileSaveBase
	{
		/// <summary>
		/// 实例化一个文件存储的基类
		/// </summary>
		// Token: 0x0600202A RID: 8234 RVA: 0x0009D4D8 File Offset: 0x0009B6D8
		public SoftFileSaveBase()
		{
			this.HybirdLock = new SimpleHybirdLock();
		}

		/// <summary>
		/// 在日志保存时的标记当前调用类的信息
		/// </summary>
		// Token: 0x170006A4 RID: 1700
		// (get) Token: 0x0600202B RID: 8235 RVA: 0x0009D4ED File Offset: 0x0009B6ED
		// (set) Token: 0x0600202C RID: 8236 RVA: 0x0009D4F5 File Offset: 0x0009B6F5
		protected string LogHeaderText { get; set; }

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.ISoftFileSaveBase.ToSaveString" />
		// Token: 0x0600202D RID: 8237 RVA: 0x000617BE File Offset: 0x0005F9BE
		public virtual string ToSaveString()
		{
			return string.Empty;
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.ISoftFileSaveBase.LoadByString(System.String)" />
		// Token: 0x0600202E RID: 8238 RVA: 0x00009AA1 File Offset: 0x00007CA1
		public virtual void LoadByString(string content)
		{
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.ISoftFileSaveBase.LoadByFile" />
		// Token: 0x0600202F RID: 8239 RVA: 0x0009D4FE File Offset: 0x0009B6FE
		public virtual void LoadByFile()
		{
			this.LoadByFile((string m) => m);
		}

		/// <summary>
		/// 使用用户自定义的解密方法从文件读取数据
		/// </summary>
		/// <param name="decrypt">用户自定义的解密方法</param>
		// Token: 0x06002030 RID: 8240 RVA: 0x0009D528 File Offset: 0x0009B728
		public void LoadByFile(Converter<string, string> decrypt)
		{
			bool flag = this.FileSavePath != "";
			if (flag)
			{
				bool flag2 = File.Exists(this.FileSavePath);
				if (flag2)
				{
					this.HybirdLock.Enter();
					try
					{
						using (StreamReader streamReader = new StreamReader(this.FileSavePath, Encoding.Default))
						{
							this.LoadByString(decrypt(streamReader.ReadToEnd()));
						}
					}
					catch (Exception ex)
					{
						ILogNet ilogNet = this.ILogNet;
						if (ilogNet != null)
						{
							ilogNet.WriteException(this.LogHeaderText, StringResources.Language.FileLoadFailed, ex);
						}
					}
					finally
					{
						this.HybirdLock.Leave();
					}
				}
			}
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.ISoftFileSaveBase.SaveToFile" />
		// Token: 0x06002031 RID: 8241 RVA: 0x0009D604 File Offset: 0x0009B804
		public virtual void SaveToFile()
		{
			this.SaveToFile((string m) => m);
		}

		/// <summary>
		/// 使用用户自定义的加密方法保存数据到文件
		/// </summary>
		/// <param name="encrypt">用户自定义的加密方法</param>
		// Token: 0x06002032 RID: 8242 RVA: 0x0009D630 File Offset: 0x0009B830
		public void SaveToFile(Converter<string, string> encrypt)
		{
			bool flag = this.FileSavePath != "";
			if (flag)
			{
				this.HybirdLock.Enter();
				try
				{
					using (StreamWriter streamWriter = new StreamWriter(this.FileSavePath, false, Encoding.Default))
					{
						streamWriter.Write(encrypt(this.ToSaveString()));
						streamWriter.Flush();
					}
				}
				catch (Exception ex)
				{
					ILogNet ilogNet = this.ILogNet;
					if (ilogNet != null)
					{
						ilogNet.WriteException(this.LogHeaderText, StringResources.Language.FileSaveFailed, ex);
					}
				}
				finally
				{
					this.HybirdLock.Leave();
				}
			}
		}

		/// <summary>
		/// 文件存储的路径
		/// </summary>
		// Token: 0x170006A5 RID: 1701
		// (get) Token: 0x06002033 RID: 8243 RVA: 0x0009D704 File Offset: 0x0009B904
		// (set) Token: 0x06002034 RID: 8244 RVA: 0x0009D70C File Offset: 0x0009B90C
		public string FileSavePath { get; set; }

		/// <summary>
		/// 日志记录类
		/// </summary>
		// Token: 0x170006A6 RID: 1702
		// (get) Token: 0x06002035 RID: 8245 RVA: 0x0009D715 File Offset: 0x0009B915
		// (set) Token: 0x06002036 RID: 8246 RVA: 0x0009D71D File Offset: 0x0009B91D
		public ILogNet ILogNet { get; set; }

		// Token: 0x040007AE RID: 1966
		private SimpleHybirdLock HybirdLock;
	}
}
